const app = getApp()

import {
  img_scrool_list,
} from '../../api/request'

Page({
  data: {
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 500,
    boardcast_info: [],
    img_scrool_list: [],

    img_url: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2964996682,1010300756&fm=15&gp=0.jpg',

    top: 0, //上下偏移量
    left: 0, //左右偏移量
    distance: 0,
    scale: 1,
    baseWidth: null,
    baseHeight: null,
    scaleWidth: null,
    scaleHeight: null,
    start_point: [0, 0], //开始坐标
  },

  touchstartCallback1: function (e) {
    // 单手指缩放开始，也不做任何处理
    if (e.touches.length == 1)
      return
    console.log('双手指触发开始')
    // 注意touchstartCallback 真正代码的开始 // 一开始我并没有这个回调函数，会出现缩小的时候有瞬间被放大过程的bug // 当两根手指放上去的时候，就将distance 初始化。 
    let xMove = e.touches[1].clientX - e.touches[0].clientX;
    let yMove = e.touches[1].clientY - e.touches[0].clientY;
    let distance = Math.sqrt(xMove * xMove + yMove * yMove);
    console.log('distance =', distance)
    this.setData({
      distance: distance,
    })
  },

  touchstartCallback: function (e) {
    this.setData({
      start_point: [e.touches[0].pageX, e.touches[0].pageY]
    })
  },

  touchmoveCallback: function (e) {
    let touch = this.data.touch
    var m_step = 30
    // 单手指缩放移动位置 
    if (e.touches.length == 1) {
      var cur_point = [e.touches[0].pageX, e.touches[0].pageY]
      var start_point = this.data.start_point
      if (cur_point[0] <= start_point[0]) {
        if (Math.abs(cur_point[0] - start_point[0]) >= Math.abs(cur_point[1] - start_point[1])) {
          this.setData({
            left: this.data.left - Math.abs(cur_point[0] - start_point[0]) / m_step
          })
        } else {
          if (cur_point[1] >= start_point[1]) {
            this.setData({
              top: this.data.top + Math.abs(cur_point[1] - start_point[1]) / m_step
            })
          } else {
            this.setData({
              top: this.data.top - Math.abs(cur_point[1] - start_point[1]) / m_step
            })
          }
        }
      } else {
        if (Math.abs(cur_point[0] - start_point[0]) >= Math.abs(cur_point[1] - start_point[1])) {
          this.setData({
            left: this.data.left + Math.abs(cur_point[0] - start_point[0]) / m_step
          })
        } else {
          if (cur_point[1] >= start_point[1]) {
            this.setData({
              top: this.data.top + Math.abs(cur_point[1] - start_point[1]) / m_step
            })
          } else {
            this.setData({
              top: this.data.top - Math.abs(cur_point[1] - start_point[1]) / m_step
            })
          }
        }
      }
      return
    }

    console.log('双手指运动')
    let xMove = e.touches[1].clientX - e.touches[0].clientX;
    let yMove = e.touches[1].clientY - e.touches[0].clientY;
    // 新的 ditance 
    let distance = Math.sqrt(xMove * xMove + yMove * yMove);
    let distanceDiff = distance - this.data.distance;
    let newScale = this.data.scale + 0.005 * distanceDiff
    // 为了防止缩放得太大，所以scale需要限制，同理最小值也是
    if (newScale >= 2) {
      newScale = 2
    }

    if (newScale <= 0.6) {
      newScale = 0.6
    }

    let scaleWidth = newScale * this.data.baseWidth
    let scaleHeight = newScale * this.data.baseHeight
    // 赋值 新的 => 旧的 
    this.setData({
      distance: distance,
      scale: newScale,
      scaleWidth: scaleWidth,
      scaleHeight: scaleHeight,
      diff: distanceDiff
    })
    console.log('distance=', distance)
  },

  bindload: function (e) {
    // bindload 这个api是<image>组件的api类似<img>的onload属性 
    this.setData({
      baseWidth: e.detail.width,
      baseHeight: e.detail.height,
      scaleWidth: e.detail.width,
      scaleHeight: e.detail.height
    })
  },

  //轮播
  img_scrool_list(data) {
    data = {
      para: [data, "HOME"]
    }

    img_scrool_list(data).then(res => {
      this.setData({
        img_scrool_list: res.data
      })
    })
  },


  img_url_input(e) {
    this.setData({
      img_url: e.detail.value,
    })
  },


  onLoad: function (options) {
    this.img_scrool_list(app.globalData.cur_property_code); //刷新图片滚动
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})